import { createRouter, createWebHistory } from 'vue-router'
import HomeView from '../views/HomeView.vue'
import { ElMessage } from 'element-plus';

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      name: 'Login',
      component: () => import('../views/LoginView.vue'),
    },
    {
      path: '/MianView',
      name: 'Mian',
      component: () => import('../views/MianView.vue'),
      children: [
        {
          path: '/BasicInformation/BasicView',
          name: 'BasicInformation/BasicView',
          component: () => import('../views/BasicInformation/BasicView.vue')
        },
        {
          path: '/BasicInformation/Large_screen_displayView',
          name: 'BasicInformation/Large_screen_display',
          component: () => import('../views/BasicInformation/Large_screen_displayView.vue')
        },
        {
          path: '/BasicInformation/VendorView',
          name: 'BasicInformation/VendorView',
          component: () => import('../views/BasicInformation/VendorView.vue')
        },
        {
          path: '/TotalInventory/TotalView',
          name: 'TotalInventory/TotalView',
          component: () => import('../views/TotalInventory/TotalView.vue')
        },
        {
          path: '/RBAC/UserView',
          name: 'RBAC/UserView',
          component: () => import('../views/RBAC/UserView.vue')
        },
        {
          path: '/RBAC/RoleUserView',
          name: 'RBAC/RoleUserView',
          component: () => import('../views/RBAC/RoleUserView.vue')
        },
        {
          path: '/WorkflowEngine/SettingsEngineView',
          name: 'WorkflowEngine/SettingsEngine',
          // @ts-ignore
          component: () => import('../views/WorkflowEngine/SettingsEngineView.vue')
        },
        {
          path: '/BasicInformation/WarehouseLocationView',
          name: 'BasicInformation/WarehouseLocationView',
          component: () => import('../views/BasicInformation/WarehouseLocationView.vue')
        },
        {
          path: '/BasicInformation/Large_screen_displayView',
          name: 'BasicInformation/Large_screen_display',
          component: () => import('../views/BasicInformation/Large_screen_displayView.vue')
        },
        {
          path: '/GoodsInfoView/GoodsInfoQuery',
          name: 'GoodsInfoView/GoodsInfoQuery',
          component: () => import('../views/GoodsInfoView/GoodsInfoQuery.vue')
        },

      ]
    }

  ]
})

export default router

router.beforeEach((to, from, next) => {
  const token = localStorage.getItem('token');

  // 判断目标页面是否需要登录权限
  if (to.path !== '/' && !token) {
    // 如果没有 token 且不是去登录页，强制跳转到登录页
    ElMessage.error("请先登录亲!");
    next({ path: '/' });
  } else {
    next(); // 允许通过
  }
});